//
//  DetailKindMoreController.m
//  MarianasTrench
//
//  Created by 王晓晨 on 16/3/21.
//  Copyright © 2016年 王爵. All rights reserved.
//

#import "DetailKindMoreController.h"

@interface DetailKindMoreController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *bigCollection;
@property (nonatomic, strong) NSMutableArray *arrModel;/**< model数组 >*/
@property (nonatomic, strong) NSString *httpStr;/**< 接口字符串 >*/
@property (nonatomic, assign) NSInteger start;/**< 开始位置 >*/
@property (nonatomic, assign) NSInteger toPage;/**< 结束位置 >*/
@property (nonatomic, assign) BOOL isUpState;/**< 是否向上状态 >*/
@property (nonatomic, strong) NSString *segType;/**< segment >*/

@end

@implementation DetailKindMoreController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"全部";
    self.view.backgroundColor = [UIColor whiteColor];
    self.arrModel = [NSMutableArray array];
//    self.httpStr = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/category/album?calcDimension=hot&categoryId=%@&device=android&pageId=1&pageSize=20&status=0&tagName=%@", self.httpId, self.tagName];
    
    self.segType = @"hot";
    self.start = 1;
    self.toPage = 20;
    
    [self dataHandel];
    [self createSubViews];
}

#pragma mark -  创建子视图
- (void)createSubViews {
    UISegmentedControl *segMent = [[UISegmentedControl alloc] initWithItems:@[@"最火", @"最新", @"经典"]];
    segMent.frame = CGRectMake(10, 70, SCREEN_WIDTH - 20, 30);
    segMent.backgroundColor = [UIColor whiteColor];
    segMent.tintColor = [UIColor redColor];
    [self.view addSubview:segMent];
    [segMent addTarget:self action:@selector(segMentAction:) forControlEvents:UIControlEventValueChanged];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(SCREEN_WIDTH - 20, 80);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.bigCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 100, SCREEN_WIDTH, SCREEN_HETGHT - 153) collectionViewLayout:layout];
    [self.view addSubview:_bigCollection];
    _bigCollection.backgroundColor = [UIColor whiteColor];
    _bigCollection.delegate = self;
    _bigCollection.dataSource = self;
    [_bigCollection registerClass:[SmallDetailKindCollectionCell class] forCellWithReuseIdentifier:@"SmallDetailKindCollectionCell"];
    
    [self addFooter];
    [self addHeader];
}

#pragma mark - segment点击方法
- (void)segMentAction:(UISegmentedControl *)segMent {
    
    if (segMent.selectedSegmentIndex == 0) {
        self.segType = @"hot";
        [self.bigCollection.mj_header beginRefreshing];
    }
    if (segMent.selectedSegmentIndex == 1) {
        self.segType = @"recent";
        [self.bigCollection.mj_header beginRefreshing];
    }
    if (segMent.selectedSegmentIndex == 2) {
        self.segType = @"classic";
        [self.bigCollection.mj_header beginRefreshing];
    }
    
}

#pragma mark - itme个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _arrModel.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SmallDetailKindCollectionCell *Cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SmallDetailKindCollectionCell" forIndexPath:indexPath];
    CellArrDetailKindModel *model = _arrModel[indexPath.row];
    Cell.cellArrDetailKindModel = model;
    return Cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    WJCollectionViewController *WJVc = [[WJCollectionViewController alloc] init];
    CellArrDetailKindModel *model = _arrModel[indexPath.row];
    WJVc.str = model.albumId;
    [self.navigationController pushViewController:WJVc animated:YES];
}

#pragma mark - 数据解析
- (void)dataHandel {
    
    self.httpStr = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/category/album?calcDimension=%@&categoryId=%@&device=android&pageId=%ld&pageSize=%ld&status=0&tagName=%@", self.segType, self.httpId, self.start, self.toPage, self.tagName];
    
    [APPTools GETWithURL:_httpStr par:nil success:^(id responseObject) {
        
        if (_isUpState == NO) {
            [self.bigCollection.mj_header endRefreshing];
            
            [_arrModel removeAllObjects];
        }
        NSMutableArray *tempArr = [NSMutableArray array];
        tempArr = [CellArrDetailKindModel modelHandleWithArray:responseObject[@"list"]];
        
        for (CellArrDetailKindModel *model in tempArr) {
            [self.arrModel addObject:model];
        }
        
        [self.bigCollection.mj_footer endRefreshing];
        
        [_bigCollection reloadData];
        
    } filed:^(NSError *error) {
        
    }];
    
}

#pragma mark - 上拉下拉
- (void)addHeader {
    self.bigCollection.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
       
        _isUpState = NO;
        self.start = 1;
        self.toPage = 20;
        
        [self dataHandel];
    }];
    
    // 马上进入刷新状态
    [self.bigCollection.mj_header beginRefreshing];
}

- (void)addFooter {
    self.bigCollection.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _isUpState = YES;
        self.start += 20;
        self.toPage += 20;
        
        [self dataHandel];
    }];

    [self.bigCollection.mj_footer beginRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
